# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.6.0)
project("webcrypto" LANGUAGES C)

enable_language(ASM)

# Set as required by android-sources.cmake included below
set(BORINGSSL_ROOT ../third_party/boringssl/)

# Import sources as generated by tool/update-boringssl.py
# This provides variables, and requires BORINGSSL_ROOT to be set.
#  - crypto_sources
#  - crypto_sources_linux_aarch64
#  - crypto_sources_linux_arm
#  - crypto_sources_linux_ppc64le
#  - crypto_sources_linux_x86
#  - crypto_sources_linux_x86_64
include(
  ../third_party/boringssl/sources.cmake
)

# Tradeoff performance for smaller binary size, see:
# https://boringssl.googlesource.com/boringssl/+/HEAD/BUILDING.md#binary-size
add_definitions(-DOPENSSL_SMALL)

# TODO: Enable link time optimization
#       Requires fixing https://github.com/google/webcrypto.dart/issues/80
# set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -flto")
# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto")

# Android ABIs, see also:
# https://developer.android.com/ndk/guides/abis
if(ANDROID_ABI STREQUAL "armeabi-v7a")
  set(crypto_sources_ARCH ${crypto_sources_linux_arm})
elseif ( ANDROID_ABI STREQUAL "arm64-v8a")
  set(crypto_sources_ARCH ${crypto_sources_linux_aarch64})
elseif ( ANDROID_ABI STREQUAL "x86")
  set(crypto_sources_ARCH ${crypto_sources_linux_x86})
elseif ( ANDROID_ABI STREQUAL "x86_64")
  set(crypto_sources_ARCH ${crypto_sources_linux_x86_64})
else()
  # If we can't match an architecture, we just disable assembler optimizations
  set(crypto_sources_ARCH)
  add_definitions(-DOPENSSL_NO_ASM)
endif()

add_library(
  webcrypto
  
  # Build a shared library
  SHARED
  
  # Source files
  ../src/webcrypto.c
  ../src/symbols.generated.c
  ${crypto_sources}
  ${crypto_sources_ARCH}
)

target_include_directories(
  webcrypto

  PRIVATE

  ../third_party/boringssl/src/include/
)

set_target_properties(
  webcrypto

  PROPERTIES
  
  C_VISIBILITY_PRESET hidden
  VISIBILITY_INLINES_HIDDEN 1
)
